package io.gitee.thghh.chronicon.swing.view;

import com.formdev.flatlaf.FlatLaf;
import com.formdev.flatlaf.extras.FlatAnimatedLafChange;
import com.formdev.flatlaf.extras.FlatSVGIcon;
import com.formdev.flatlaf.util.LoggingFacade;
import com.jidesoft.dialog.JideOptionPane;
import io.gitee.thghh.chronicon.swing.components.AdjustableUI;
import io.gitee.thghh.chronicon.swing.theme.DarkTheme;
import io.gitee.thghh.chronicon.swing.theme.GlazzedTheme;
import io.gitee.thghh.chronicon.swing.theme.LightTheme;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * 标题菜单栏
 *
 * @author pengzhikang
 * @date 2025/03/22
 * @since 1.0.0
 */
public class TitleMenuBar extends JMenuBar implements AdjustableUI {

    private MainFrame mainFrame;
    private JButton themeBut;

    private JButton noticeBut;
    private JLabel titleLabel;

    private JPopupMenu themePopupMenu;
    private JPopupMenu personalPopupMenu;

    public TitleMenuBar(MainFrame mainFrame) {
        this.mainFrame = mainFrame;

        adjustUI();
    }

    @Override
    public void adjustUI() {
        setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));

        // left
        add(getTitleLabel());
        add(Box.createGlue());
        // right
        add(getThemeBut());
        add(getNoticeBut());
        add(getUserBut());
    }

    @Override
    public void updateUI() {
        super.updateUI();
        if (themePopupMenu != null) {
            SwingUtilities.updateComponentTreeUI(themePopupMenu);
        }
        if (personalPopupMenu != null) {
            SwingUtilities.updateComponentTreeUI(personalPopupMenu);
        }
    }

    public JButton getNoticeBut() {
        if (noticeBut == null) {
            noticeBut = new JButton();
            noticeBut.setIcon(new FlatSVGIcon("icons/bell.svg", 25, 25));
            noticeBut.putClientProperty("JButton.buttonType", "toolBarButton");
            noticeBut.setFocusable(false);
            noticeBut.addActionListener(e -> {
                JideOptionPane.showConfirmDialog(mainFrame, "我的消息");
            });

        }
        return noticeBut;
    }

    public JButton getThemeBut() {
        if (themeBut == null) {


            themeBut = new JButton();
            themeBut.putClientProperty("JButton.buttonType", "toolBarButton");

            themeBut.setIcon(new FlatSVGIcon("icons/skin.svg", 26, 26));
            themeBut.addActionListener(e -> showPopupSkinButtonActionPerformed(e));
        }
        return themeBut;
    }

    public JLabel getTitleLabel() {
        if (titleLabel == null) {
            titleLabel = new JLabel("代码生成器");
            titleLabel.putClientProperty("FlatLaf.styleClass", "h1");
            titleLabel.setIcon(new FlatSVGIcon("icons/guanli.svg", 45, 45));
            titleLabel.setBorder(BorderFactory.createEmptyBorder(7, 3, 7, 7));

        }
        return titleLabel;
    }

    public JButton getUserBut() {
        FlatSVGIcon svgIcon = new FlatSVGIcon("icons/user.svg", 25, 25);
//        svgIcon.setColorFilter(new FlatSVGIcon.ColorFilter(color -> {
//            return new Color(0xffffff);
//        }));
        JButton userBut = new JButton();
        userBut.setIcon(svgIcon);
        userBut.putClientProperty("JButton.buttonType", "toolBarButton");
        userBut.setFocusable(false);
        userBut.setFont(userBut.getFont().deriveFont(18f));
        userBut.addActionListener(e -> showPopupMenuButtonActionPerformed((Component) e.getSource()));
        return userBut;
    }

    /**
     * 皮肤显示弹出按钮执行操作
     *
     * @param e e
     */
    private void showPopupSkinButtonActionPerformed(ActionEvent e) {
        Component invoker = (Component) e.getSource();
        if (themePopupMenu == null) {

            themePopupMenu = new JPopupMenu();
            ButtonGroup group = new ButtonGroup();

            JCheckBoxMenuItem lighterMenuItem = new JCheckBoxMenuItem("白色");
            group.add(lighterMenuItem);
            themePopupMenu.add(lighterMenuItem);
            lighterMenuItem.addActionListener(e1 -> {
                theme("白色");
                updateUserTheme("白色");
            });


            JCheckBoxMenuItem darkMenuItem = new JCheckBoxMenuItem("深色");
            darkMenuItem.addActionListener(e1 -> {
                theme("深色");
                updateUserTheme("深色");
            });
            themePopupMenu.add(darkMenuItem);
            group.add(darkMenuItem);

            JCheckBoxMenuItem glazzedMenuItem = new JCheckBoxMenuItem("玻璃");
            glazzedMenuItem.addActionListener(e1 -> {
                theme("玻璃");
                updateUserTheme("玻璃");
            });
            themePopupMenu.add(glazzedMenuItem);
            group.add(glazzedMenuItem);

        }


        themePopupMenu.show(invoker, 0, invoker.getHeight());

    }

    /**
     * 主题
     */
    public void theme(String theme) {

        EventQueue.invokeLater(() -> {
            FlatAnimatedLafChange.showSnapshot();
            try {
                switch (theme) {
                    case "深色": {
                        UIManager.setLookAndFeel(DarkTheme.class.getName());
                        break;
                    }
                    case "玻璃": {
                        UIManager.setLookAndFeel(GlazzedTheme.class.getName());
                        break;
                    }
                    case "白色":
                    default: {
                        UIManager.setLookAndFeel(LightTheme.class.getName());
                        break;
                    }
                }
            } catch (Exception ex) {
                LoggingFacade.INSTANCE.logSevere(null, ex);
            }

            mainFrame.adjustStyle();

            FlatLaf.updateUI();
            FlatSVGIcon.ColorFilter.getInstance().add(new Color(0x6e6e6e), new Color(0x6e6e6e), new Color(0xffffff));
            FlatAnimatedLafChange.hideSnapshotWithAnimation();
        });


    }

    /**
     * 显示弹出菜单按钮执行操作
     */
    private void showPopupMenuButtonActionPerformed(Component invoker) {
        if (personalPopupMenu == null) {
            personalPopupMenu = new JPopupMenu();
            JPanel infoPanel = new JPanel(new BorderLayout());
            JLabel label = new JLabel("", JLabel.CENTER);
            label.setIcon(new FlatSVGIcon("icons/user.svg", 80, 80));
            label.setText("admin");
            label.setVerticalTextPosition(SwingConstants.BOTTOM);  //必须设置文字树直方向位置
            label.setHorizontalTextPosition(SwingConstants.CENTER);
            infoPanel.add(label, BorderLayout.CENTER);
            infoPanel.add(new JLabel("系统管理员", JLabel.CENTER), BorderLayout.SOUTH);
            label.setPreferredSize(new Dimension(240, 100));
            JMenuItem menuItem9 = new JMenuItem("个人信息");
            menuItem9.setIcon(new FlatSVGIcon("icons/gerenxinxi.svg", 25, 25));
            JMenuItem menuItem11 = new JMenuItem("退出");
            menuItem11.setIcon(new FlatSVGIcon("icons/logout.svg", 25, 25));

            menuItem11.addActionListener(e1 -> JideOptionPane.showConfirmDialog(mainFrame, "退出"));
            menuItem9.addActionListener(e1 -> {
                int tabIndex = MainFrame.getInstance().getTabbedPane().indexOfTab("个人信息");
            });

            personalPopupMenu.add(infoPanel);
            personalPopupMenu.addSeparator();
            personalPopupMenu.add(menuItem9);
            personalPopupMenu.addSeparator();
            personalPopupMenu.add(menuItem11);
        }


        personalPopupMenu.show(invoker, 0, invoker.getHeight());

    }

    public void updateUserTheme(String theme) {
        SwingWorker<String, String> stringSwingWorker = new SwingWorker<String, String>() {
            @Override
            protected String doInBackground() throws Exception {
//                String key = "swing.theme.userid." + AppStore.getAuthPermissionInfoRespVO().getUser().getId();
//                ConfigRespVO configRespVO = Request.connector(ConfigFeign.class).getConfig(key).getCheckedData();
//                if (configRespVO == null) {
//                    ConfigSaveReqVO saveReqVO = new ConfigSaveReqVO();
//                    saveReqVO.setKey(key);
//                    saveReqVO.setValue(theme);
//                    saveReqVO.setVisible(true);
//                    saveReqVO.setCategory("ui");
//                    saveReqVO.setName("用户主题");
//                    Request.connector(ConfigFeign.class).createConfig(saveReqVO);
//                } else {
//                    ConfigSaveReqVO saveReqVO = new ConfigSaveReqVO();
//                    saveReqVO.setId(configRespVO.getId());
//                    saveReqVO.setName(configRespVO.getName());
//                    saveReqVO.setCategory(configRespVO.getCategory());
//                    saveReqVO.setValue(theme);
//                    saveReqVO.setKey(configRespVO.getKey());
//                    saveReqVO.setVisible(true);
//                    Request.connector(ConfigFeign.class).updateConfig(saveReqVO);
//                }
                return "";
            }
        };
        stringSwingWorker.execute();

    }
}
